package com.kcl.dfss;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.kcl.dfss.adapter.DeviceAdapter;
import com.kcl.dfss.http.CameraConnection;
import com.kcl.dfss.http.CameraConnection.ResponseHandler;
import com.kcl.dfss.http.ServerConnection;
import com.kcl.dfss.utils.AppUtils;
import com.kcl.dfss.utils.WiFiManagerUtils;

public class DeviceSetActivity extends Activity {
	
	private static final String tag = "DeviceSetActivity";
	private String devicename,carmodel,deviceId,supplier;
	private String rom_version,rom_versionold;
	private boolean cameraconnect = false;
	private RelativeLayout device_noconnect,layout_nodevice;
	private ListView device_list1;
	private TextView text_devicename,text_nodevice;
	private Button btn_adddevice;
	private Handler handler = null;
	private Message message = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_device_set);
		
		btn_adddevice = (Button) findViewById(R.id.btn_adddevice);
		
		final SharedPreferences preferences = getSharedPreferences("shared_login", 0);
		final String userid = preferences.getString("userid", "");
		rom_versionold = preferences.getString("romversion", "");
		final String deviceid = "";
		
		Intent intent = getIntent();
		carmodel = intent.getStringExtra("carmodel");
		
		final CameraConnection cameraConnection = new CameraConnection(new ResponseHandler(){

			@Override
			public void handleResponse(int request_code, String response) {
				Log.d(tag, "修改名称response:"+response);
				Log.d(tag, "修改名称request_code:"+request_code);
				switch(request_code){
				case CameraConnection.CODE_FIRMWARE_VERSION:
					SharedPreferences preferences = getSharedPreferences("shared_login", 0);
					preferences.edit().putString("camera_romversion", response).commit();
					break;
				}
			}}, this);
		
		cameraConnection.getFirmwareVersion();
		
		cameraconnect = LocationService.getCamemraConnected();
		Log.i(tag, "设备连接："+cameraconnect);
		device_list1 = (ListView) findViewById(R.id.device_list1);
		device_noconnect = (RelativeLayout) findViewById(R.id.device_noconnect);
		if(cameraconnect){
			device_list1.setVisibility(View.VISIBLE);
			device_noconnect.setVisibility(View.GONE);
			//当前设备
			devicename = WiFiManagerUtils.getWiFiName(DeviceSetActivity.this).replace("\"", "");
			
			ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String,String>>();
			String data[] = {"设备名称,"+devicename,"修改设备密码, ","用户查看, ","安装车辆类型,"+carmodel,"设备安装中轴线, ",
					"设备ID,1234567890","供应商信息,径卫视觉","ROM管理"};
			int type[] = {0,0,0,0,0,1,1,2};
			for(int i=0;i<data.length;i++){
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("data", data[i]);
				listItem.add(map);
			}
			DeviceAdapter deviceAdapter = new DeviceAdapter(listItem, this, type);
			device_list1.setAdapter(deviceAdapter);
			AppUtils.setListViewHeight(device_list1);
			device_list1.setOnItemClickListener(new OnItemClickListener() {
				
				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					switch (position) {
					case 0:
						Intent intent = new Intent();
						intent.putExtra("devicename", devicename);
						intent.setClass(DeviceSetActivity.this, SetDeviceNameActivity.class);
						startActivity(intent);
						break;
					case 1:
						Intent intent1 = new Intent();
						intent1.setClass(DeviceSetActivity.this, SetDevicePwdActivity.class);
						startActivity(intent1);
						break;
					case 3:
						Intent intent3 = new Intent();
						intent3.setClass(DeviceSetActivity.this, SelectCarActivity.class);
						startActivity(intent3);
						break;
					case 7:
						Intent intent7 = new Intent();
						intent7.putExtra("isconnect", true);
						intent7.setClass(DeviceSetActivity.this, UpdateRomActivity.class);
						startActivity(intent7);
						break;
					default:
						break;
					}
					
				}
			});		
			
			//判断配置文件是否为空
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					String result = cameraConnection.getCamcfg();
					try {
						JSONObject jsonObject = new JSONObject(result);
						final String positionZ = jsonObject.getString("positionZ");
						final String positionX = jsonObject.getString("positionX");
						final String positionY = jsonObject.getString("positionY");
						final String vehicleWidth = jsonObject.getString("vehicleWidth");
						final String versionNum = jsonObject.getString("versionNum");
						final String pan = jsonObject.getString("pan");
						final String brandID = jsonObject.getString("brandID");
						final String seriesID = jsonObject.getString("seriesID");
						final String modelID = jsonObject.getString("modelID");
						final String tilt = jsonObject.getString("tilt");
						final String roll = jsonObject.getString("roll");
						runOnUiThread(new Runnable() {
							public void run() {
								if(positionX.equals("0") || positionY.equals("0") || positionZ.equals("0") || vehicleWidth.equals("0") || 
										versionNum.equals("0") || pan.equals("0") || brandID.equals("0") || seriesID.equals("0") || 
										modelID.equals("0") || tilt.equals("0") || roll.equals("0")){
									btn_adddevice.setText("重新配置设备");
								}else{
									btn_adddevice.setText("添加设备");
								}
							}
						});
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			}).start();
		}else{
			btn_adddevice.setText("添加设备");
			text_devicename = (TextView) findViewById(R.id.text_devicename);
			text_devicename.setText("当前无设备连接");
			device_list1.setVisibility(View.GONE);
			device_noconnect.setVisibility(View.VISIBLE);
			device_noconnect.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
						Intent intent = new Intent();
						intent.putExtra("isconnect", false);
						intent.setClass(DeviceSetActivity.this, UpdateRomActivity.class);
						startActivity(intent);
				}
			});
			
		}
		
		
				
		//历史设备
		layout_nodevice = (RelativeLayout) findViewById(R.id.layout_nodevice);
		text_nodevice = (TextView) findViewById(R.id.text_nodevice);
		final ListView device_list2 = (ListView) findViewById(R.id.device_list2);
		
		handler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				case 1:
					device_list2.setVisibility(View.VISIBLE);
					layout_nodevice.setVisibility(View.GONE);
					String arr[] = {"机器1","机器2"};
					List<Map<String,String>> listItems = new ArrayList<Map<String,String>>();
					for(int i=0;i<arr.length;i++){
						Map<String, String> map = new HashMap<String, String>();
						map.put("name", arr[i]);
						listItems.add(map);
					}
					SimpleAdapter simpleAdapter = new SimpleAdapter(DeviceSetActivity.this, listItems, R.layout.device_item4, new String[] {"name"}, new int[] {R.id.text_historydevice});
					device_list2.setAdapter(simpleAdapter);
					AppUtils.setListViewHeight(device_list2);
					device_list2.setOnItemClickListener(new OnItemClickListener() {

						@Override
						public void onItemClick(AdapterView<?> parent, View view,
								int position, long id) {
							Intent intent = new Intent();
							intent.setClass(DeviceSetActivity.this, HistoryDeviceMessage.class);
							startActivity(intent);
						}
					});
					break;
				case 2:
					device_list2.setVisibility(View.GONE);
					layout_nodevice.setVisibility(View.VISIBLE);
					text_nodevice.setText("暂没有使用过其他设备");
					break;
				default:
					break;
				}
			}
			
		};
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				final String result = ServerConnection.GetDeviceByUser(userid);
				try {
					JSONObject jsonObject = new JSONObject(result);
					final String success = jsonObject.getString("success");
					final String msg = jsonObject.getString("msg");
					if(success.equals("true")){
						message = new Message();
						message.what = 1;
						handler.sendMessage(message);
					}else if(success.equals("false")){
						message = new Message();
						message.what = 2;
						handler.sendMessage(message);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}).start();
		
		
		//返回
		LinearLayout personal_set_back = (LinearLayout) findViewById(R.id.personal_set_back);
		personal_set_back.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		//添加设备
		btn_adddevice.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				SharedPreferences addDevice = getSharedPreferences("adddevice", 0);
				addDevice.edit().putBoolean("select", true).commit();
				
				SharedPreferences camcfg = getSharedPreferences("camcfg", 0);
				camcfg.edit().putString("brandID", "").commit();
				camcfg.edit().putString("seriesID", "").commit();
				camcfg.edit().putString("modelID", "").commit();
				
				Intent intent = new Intent();
				intent.setClass(DeviceSetActivity.this, MatchingWiFiActivity.class);
				startActivity(intent);
			}
		});
	}
	
	

}
